﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DormMS.IService;
using DormMS.IService.Common;
using DormMS.Model;
using DormMS.Model.SearchModels;
using Infrastructure;
using NLog;

namespace DormMS.Controllers
{
    public class BedController : BaseController
    {
        protected IRoomService roomService;
        protected ILogService logService;
        protected IUserInfoService userInfoService;
        protected IBuildingService buildingService;
        protected ISysCodeService sysCodeService;
        protected IStudentBedService studentBedService;
        protected IBedService bedService;
        private static Logger NLog = LogManager.GetLogger("Request");
        public BedController(IUserInfoService s, IRoomService _roomService, ILogService l, IBuildingService _buildingService, ISysCodeService _sysCodeService, IStudentBedService _studentBedService, IBedService _bedservice) : base(s)
        {
            roomService = _roomService;
            logService = l;
            userInfoService = s;
            buildingService = _buildingService;
            sysCodeService = _sysCodeService;
            studentBedService = _studentBedService;
            bedService = _bedservice;
        }
        // GET: Bed
        public ActionResult BedList()
        {
            return View();
        }
        /// <summary>
        /// 获取公寓初期化列表
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetBedList(BedSearchModel search)
        {
            int total = 0;
            var list = bedService.GetBedList(search, ref total);
            return Json(new { Total = total, Results = list, _UserInfo = userInfoService.GetUserInfo(CurrentLoginUser.Id) });
        }

        /// <summary>
        /// 获取学生对应床位明细
        /// </summary>
        /// <returns></returns>
        public ActionResult BedDetail()
        {
            return View();
        }
        /// <summary>
        /// 获取学生对应床位初期化明细
        /// </summary>
        /// <returns></returns>
        public ActionResult GetBedInitDetail(int id)
        {

            var bedInfo = bedService.GetBedById(id);
            return Json(new { BedInfo = bedInfo, BuildingList = buildingService.GetBuildingAllList(), RoomList = roomService.GetRoomAllList(),RoomAttrList = sysCodeService.GetSysCodeByCodeType("公寓性质"), RoomTypeList = sysCodeService.GetSysCodeByCodeType("公寓类型"), });
        }

        /// <summary>
        /// 获取学生对应床位初期化明细
        /// </summary>
        /// <returns></returns>
        public ActionResult BedCheck(string BedNO, int BuildingId, int RoomId)
        {

            Result result = new Result();
            int errorType = 0;
            int bedNum = 0;
            var entity = bedService.GetBedListByBedNOAndBuildingIDAndRoomID(BedNO, BuildingId, RoomId);

            if (entity == null)
            {
                result.isSuccess = true;
            }
            
            Room room = roomService.GetRoomInfoById(RoomId);
            
            List<Bed> bedList = bedService.GetBedListByRoomID(RoomId);

            if(room.RoomType == 1)
            {
                bedNum = 2;
            }
            else if(room.RoomType == 2)
            {
                bedNum = 4;
            }
            else if(room.RoomType == 3)
            {
                bedNum = 6;
            }
            else if(room.RoomType == 4)
            {
                bedNum = 8;
            }


            if(bedList.Count() > bedNum)
            {
                errorType = 1;
            }

            return Json(new { result = result, errorType = errorType });
        }

        /// <summary>
        /// 获取学生对应床位初期化明细
        /// </summary>
        /// <returns></returns>
        public ActionResult BedSave(Bed model)
        {

            Result result = new Result();
            try
            {

                var entity = bedService.GetBedById(model.Id);
                int rstcount = 0;
                if (entity == null)
                {
                    model.AddUserId = CurrentLoginUser.Id;
                    model.AddTime = DateTime.Now;
                    rstcount = bedService.BedAdd(model);
                    result.isSuccess = true;

                }
                else
                {
                    entity.BedNO = model.BedNO;
                    entity.BuildingId = model.BuildingId;
                    entity.RoomId = model.RoomId;
                    entity.CupboardNO = model.CupboardNO;
                    entity.EditUserId = CurrentLoginUser.Id;
                    entity.EditTime = DateTime.Now;
                    rstcount = bedService.BedEdit(model);
                    result.isSuccess = true;

                }

            }
            catch (Exception ex)
            {
                result.msg = "保存公寓信息失败！";
                NLog.Debug("保存公寓信息失败：" + ex.Message + "，" + ex.StackTrace);
            }
            return Json(result);
        }
    }
}